#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
 
typedef struct sockaddr SA;
#define BUFFSIZE 512
int sockfd = 0;						 //定义全局的listenfd
 
 
int main(int argc, char* argv[]) {
	if (argc != 3) {
		fprintf(stderr, "usage: argv[0]<ip port>\n", argv[0]);
		return 1;
	}
	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (-1 == sockfd) {
		perror("socket");
		return 2;
	} 
    
    short port = atoi(argv[2]);
    const char* ip = argv[1];
 
	//获取服务器协议地址簇
	struct sockaddr_in serv_addr; 
	memset(&serv_addr,  0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(port);
 
	inet_pton(AF_INET, ip, &serv_addr.sin_addr);
	//然后就是循环发送数据
	char buffer[BUFFSIZE];
	while (1) {
		printf("请说>>: ");
		scanf("%s", buffer);
		sendto(sockfd, buffer, strlen(buffer), 0,
			(SA*)&serv_addr, sizeof(serv_addr));
	}
 
	return 0;
}